package com.xingmang.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.xingmang.admin.service.UserService;
import com.xingmang.common.constant.ResponseData;
import com.xingmang.common.constant.ResultBean;
import com.xingmang.common.dto.admin.LoginDTO;
import com.xingmang.common.dto.admin.RegisterDTO;
import com.xingmang.common.vo.admin.LoginUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @auther: hansom
 * @Version: 1.0
 * @create 2023/6/20 10:40
 */
@RestController
@RequestMapping("/admin/login")
@Api(tags = "登录")
public class LoginController {
    @Resource
    private UserService userService;

    @PostMapping("in")
    @ApiOperation("登录")
    public ResultBean login(@RequestBody @Validated LoginDTO dto){
        return userService.login(dto);
    }

    @PostMapping("getUserInfo")
    @ApiOperation("获取用户信息")
    public ResultBean<LoginUserVO> getUserInfo(){
        return userService.getUserInfo();
    }

    @PostMapping("register")
    @ApiOperation("注册")
    public ResultBean register(@RequestBody @Validated RegisterDTO dto){
        return userService.register(dto);
    }

    @PostMapping("logout")
    @ApiOperation("退出登录")
    public ResultBean logout(){
        StpUtil.logout();
        return ResponseData.success(null);
    }
}
